package org.wattdepot.cli.command;

import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Processes list commands.
 * 
 * @author Kendyll Doi, Kimberly Heu
 * 
 */
public class WattSummary {

  /**
   * Gives the Summary information for a wattDepot Source.
   * 
   * @param sourceName The is source name.
   * @param client The WattDepot client.
   * @return a string with the summary of a source.
   */
  public static String wattSummary(String sourceName, WattDepotClient client) {
    String output = null;
    try {

      Source summarySource = client.getSource(sourceName);
      output = "Subsource: ";
      StringBuffer buf;
      if (summarySource.isSetSubSources()) {
        buf = new StringBuffer();
        for (String subsources : summarySource.getSubSources().getHref()) {
          buf.append(subsources.substring(subsources.indexOf("SIM_")));
          buf.append(", ");
        }
        output += buf.toString();
        output += "\n";
      }
      else {
        output += "None\n";
      }

      output += "Description: " + summarySource.getDescription() + "\n";
      output +=
          "Owner: "
              + summarySource.getOwner().substring(summarySource.getOwner().indexOf("/users/") + 7)
              + "\n";
      output += "Location: " + summarySource.getLocation() + "\n";
      output += "Coordinates: " + summarySource.getCoordinates() + "\n";

      output += "Properties: ";
      if (summarySource.isSetProperties()) {
        buf = new StringBuffer();
        for (Property propertylist : summarySource.getProperties().getProperty()) {
          buf.append("(" + propertylist.getKey() + " : " + propertylist.getValue() + ") ");
        }
        output += buf.toString();
        output += "\n";
      }
      else {
        output += "None";
      }

      SourceSummary dataSummary = client.getSourceSummary(sourceName);
      output += "Earliest data: " + dataSummary.getFirstSensorData() + "\n";
      output += "Latest data: " + dataSummary.getLastSensorData() + "\n";
      output += "Total data points: " + dataSummary.getTotalSensorDatas() + "\n";

    }
    catch (ResourceNotFoundException e) {
      output = "No data found for \"" + sourceName + "\"";
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return output;

  }
  
}
